package dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import util.Conversion;

import dto.QuestionDTO;
import dto.TestQuestionDTO;

import entities.Candidate;
import entities.Question;
import entities.TestQuestion;

/**
 * Session Bean implementation class TestQuestionDAO
 */
@Stateless
public class TestQuestionDAO implements TestQuestionDAORemote {

	@PersistenceContext
	EntityManager em;
    /**
     * Default constructor. 
     */
    public TestQuestionDAO() {
    }
    
    @Override
    public void startTest (int id) {
    	//Conversion conv = new Conversion();
    	//List<TestQuestionDTO> questions = new ArrayList<TestQuestionDTO>();
    	TypedQuery<TestQuestion> query = em.createQuery("SELECT tq FROM TestQuestion tq WHERE tq.interview.id = :interviewId", TestQuestion.class);
    	List<TestQuestion> tq = query.setParameter("interviewId",id).getResultList();
    	for (TestQuestion testQuestion : tq) {
    		System.out.println("startTestL: "+testQuestion.getId());
    		testQuestion.setCandidateAnswer(0);
    		
    		em.merge(testQuestion);
    	}   	
    }
    
    @Override
    public String getLink(int id){
    	
    	TypedQuery<Candidate> query = em.createQuery("SELECT c FROM Interview i join i.candidate c where i.id = :id", Candidate.class);
    	String link = query.setParameter("id", id).getResultList().get(0).getLink();
    	return link;
    }

    public List<TestQuestionDTO> getInterviewQuestions(int interviewId) throws Exception {
    	try {
    	Conversion conv = new Conversion();
    	List<TestQuestionDTO> questions = new ArrayList<TestQuestionDTO>();
    	TypedQuery<TestQuestion> query = em.createQuery("SELECT tq FROM TestQuestion tq WHERE tq.interview.id = :interviewId", TestQuestion.class);
    	List<TestQuestion> tq = query.setParameter("interviewId", interviewId).getResultList();
    	for (TestQuestion testQuestion : tq) {
    		questions.add(conv.fromTestQuestionToTestQuestionDTO(testQuestion));
    	}
    	return questions;
    	} catch (Exception e) {
    		throw new Exception("Exception in getInterviewQuestions - in TestQuestionDAO", e);
    	}
    }

    @Override
	public QuestionDTO getQuestionByPageNumber(int page, int id) {

		TypedQuery<TestQuestion> query = em.createQuery(
				"SELECT i.testQuestions FROM Interview i WHERE i.id= :id",
				TestQuestion.class);

		query.setParameter("id", id);

		List<TestQuestion> list = query.getResultList();

		Question q = list.get(page).getQuestion();

		QuestionDTO quest = new QuestionDTO();
		quest.setId(q.getId());
		quest.setAnswer1(q.getAnswer1());
		quest.setAnswer2(q.getAnswer2());
		quest.setAnswer3(q.getAnswer3());
		quest.setAnswer4(q.getAnswer4());
		// quest.setCategory(q.getCategory());
		quest.setCorrectAnswers(q.getCorrectAnswers());
		quest.setText(q.getText());

		return quest;

	}

	@Override
	public int numberOfQuestions(int id) {

		TypedQuery<TestQuestion> query = em.createQuery(
				"SELECT i.testQuestions FROM Interview i WHERE i.id= :id",
				TestQuestion.class);

		query.setParameter("id", id);

		List<TestQuestion> list = query.getResultList();

		return list.size();
	}

	@Override
	public void updateCandidateAnswer(int id, int question, int answer) {
		TypedQuery<TestQuestion> query = em.createQuery(
				"SELECT i.testQuestions FROM Interview i WHERE i.id= :id",
				TestQuestion.class);

		query.setParameter("id",id );
		List<TestQuestion> list = query.getResultList();
		list.get(question).setCandidateAnswer(answer);	
	}
	
	@Override
	public int getCandidatesAnswer (int question) {
		TypedQuery<TestQuestion> query = em.createQuery(
				"SELECT i.testQuestions FROM Interview i WHERE i.id= :id",
				TestQuestion.class);

		query.setParameter("id", question);
		List<TestQuestion> list = query.getResultList();
		return list.get(question).getCandidateAnswer();
	}
	public String returnImageForAnswer(String correctAnswer,String candidateAnswer,String answer){
		String image="nothing.png";
		if (correctAnswer.contains(answer) && (candidateAnswer.contains(answer))) image= "check.png";
		else if (correctAnswer.contains(answer)) image="bar.png";
		else if (candidateAnswer.contains(answer)) image="cross.png";
		return image;
	}
	
	
}
